﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Diagnostics;
using System.IO;

namespace _4T
{
    public class Utils
    {
        public static bool ConvertVideoType(string baseFileName, string newFileName)
        {
            try
            {
                string ffmpeg = System.Web.HttpContext.Current.Server.MapPath("/" + System.Configuration.ConfigurationManager.AppSettings["ffmpegFile"]);
                //string filePath = ;
                if ((!File.Exists(ffmpeg)))
                {
                    return false;
                }

                string baseFilePath = System.Web.HttpContext.Current.Server.MapPath("/FileStorage/Store/VideoUpload/" + baseFileName);
                string newFilePath = System.Web.HttpContext.Current.Server.MapPath("/FileStorage/Store/VideoUpload/" + newFileName);

                Process process = new Process();

                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.FileName = ffmpeg;
                process.StartInfo.Arguments = "-i \"" + baseFilePath + "\" -c:a libvo_aacenc -threads 0 -c:v libx264 \"" + newFilePath + "\"";
                //
                process.Start();
                process.PriorityClass = ProcessPriorityClass.Idle;
                process.WaitForExit(120000);
                //
                if (File.Exists(newFilePath))
                    return true;
                else
                    return false;
            }
            catch
            {
                return false;
            }
        }
    }
}